package try01;

public class ValidNumber {
	public void tester() {
		System.out.println(isNumber("005047e+6"));
	}
	
	public boolean isNumber(String s) {
        s = s.trim();
        
        if(s.length() == 0) return false;
        
        while(s.charAt(0) == '-' || s.charAt(0) == '+') {
            s = s.substring(1);
            if(s.length() == 0) return false;
        }
        
        for(int i = 0 ; i < s.length() ; i++) {
            if(s.charAt(i) <= '9' && s.charAt(i) >= '0' || s.charAt(i) == '.' || s.charAt(i) == 'e'|| s.charAt(i) == '-') {
            }
            else {
                return false;
            }
        }
        
        System.out.println("XX");
        
        int countofdot = countof(s, '.');
        int countofe = countof(s, 'e');
        
        if(countofe > 1) return false;
        if(countofdot > 1) return false;
        if(countof(s, '-') > 1) return false;
        if(countof(s, '+') > 1) return false;
        
        if(countof(s, '+') > 0 && countof(s, '-') > 0) return false;
        
        if(countofdot == 0 && countofe == 0) {
            if(countof(s, '-') > 0) return false;
            if(countof(s, '+') > 0) return false;
            return true;
        }
        
        if(countofdot == 1 && countofe == 0 && s.length() > 1) {
            if(countof(s, '-') > 0) return false;
            if(countof(s, '+') > 0) return false;
            return true;
        }
        
        if(countofdot == 0 && countofe == 1 && s.length() > 2) {
        	
            if(s.indexOf('e') == 0 || s.indexOf('e') == s.length()-1) return false;
            if(countof(s, '-') == 1) {
                if(s.indexOf('-') != s.indexOf('e')+1) return false;
                if(s.indexOf('-') == s.length() - 1) return false;
            }
            if(countof(s, '+') == 1) {
                if(s.indexOf('+')!=s.indexOf('e')+1) return false;
                if(s.indexOf('+') == s.length() - 1) return false;
            }
            return true;
        }
        
        if(countofdot == 1 && countofe == 1 && s.length() > 3) {
        	
            if(s.indexOf('e') == 0 || s.indexOf('e') == s.length()-1) return false;
            
            if(s.indexOf('.') > s.indexOf('e')) return false;
            
            if(s.indexOf('.') == 0 && s.indexOf('e') == 1) return false;
            
            if(countof(s, '-') == 1) {
                if(s.indexOf('-') != s.indexOf('e')+1) return false;
                if(s.indexOf('-') == s.length() - 1) return false;
            }
            if(countof(s, '+') == 1) {
                if(s.indexOf('+')!=s.indexOf('e')+1) return false;
                if(s.indexOf('+') == s.length() - 1) return false;
            }
            return true;
        }
        else
            return false;
    }
    
    public int countof(String s, char c) {
        int count = 0;
        for(int i =0 ; i < s.length() ; i ++) {
            if(s.charAt(i) == c) {
                count ++;
            }
        }
        return count;
    }
}